package com.md.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.md.base.BaseRepositoryImpl;
import com.md.config.database.CustomMapper;
import com.md.domain.model.bo.QueryApiBo;
import com.md.domain.model.po.Apis;
import com.md.repository.ApiRepository;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * @author 刘骄阳
 * 2022-12-21 16:10
 */
@Repository
public class ApiRepositoryImpl extends BaseRepositoryImpl<Apis> implements ApiRepository {

  public ApiRepositoryImpl(final CustomMapper<Apis> mapper) {
    super(mapper);
  }

  @Override
  public IPage<Apis> selectPage(final QueryApiBo queryApiBo) {
    LambdaQueryWrapper<Apis> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(!(ObjectUtils.isEmpty(queryApiBo.getId())), Apis::getId, queryApiBo.getId())
      .like(!(StringUtils.isEmpty(queryApiBo.getName())), Apis::getName, queryApiBo.getName())
      .like((!(StringUtils.isEmpty(queryApiBo.getUrl()))), Apis::getUrl, queryApiBo.getUrl())
      .eq(!(ObjectUtils.isEmpty(queryApiBo.getAuth())), Apis::getAuth, queryApiBo.getAuth())
      .eq(!(ObjectUtils.isEmpty(queryApiBo.getModule())), Apis::getModule, queryApiBo.getModule());
    return getMapper().selectPage(new Page<>(queryApiBo.getPageNum(), queryApiBo.getPageSize()), wrapper);
  }

  /**
   * 根据url查询api
   *
   * @param url url
   * @return api
   */
  @Override
  public Apis queryApiByUrl(final String url) {
    LambdaQueryWrapper<Apis> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(Apis::getUrl, url);
    return getMapper().selectOne(wrapper);
  }
}
